<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/1
 * Time: 10:46
 */

namespace Admin\Controller;

class UserController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $status = I('request.status');
        $stwhere = '';
        if ($status > 0) {
            $stwhere = " and frozen=".$status."";
        }
        $m = M("user");
        $keyword = I('post.keyword');
        if ($keyword) {
            $where = "id like '%$keyword%' or nickname like '%$keyword%' or mobile like '%$keyword%'".$stwhere;
        } else {
            $where = "1=1".$stwhere."";
        }

//var_dump($where);die;
        $count = $m->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出

        $result = $m->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order("addtime desc")->select();
        $this->assign("keyword", $keyword);
        $this->assign("status", $status);
        $this->assign("result", $result);
        $this->assign("page", $show); // 赋值分页输出
        $this->display();
    }

    /**
     * 冻结
     */
    public function frozen()
    {
        $id = I('request.id');
        $frozen=M('user')->where("id='$id'")->getField('frozen');

        if($frozen==1){
            $result = M('user')->where("id='$id'")->setField('frozen', 2);
        }else{
            $result = M('user')->where("id='$id'")->setField('frozen', 1);
        }
//        echo M()->_sql();
        if ($result === false) {
            $this->error("操作失败");
        } else {
            $this->success("操作成功", 'Admin/User/index');
        }
    }


    /**
     * 导出
     */
    public function expUser()
    {
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        $m = M("user");
        $keyword = I('post.keyword');
        if ($keyword) {
            $where = "id like '%$keyword%' or nickname like '%$keyword%'";
        } else {
            $where = "1=1";
        }
        $xlsData = $m->where($where)->order("addtime desc")->field(array('id','nickname','mobile','frozen'))->select();
        foreach ($xlsData as $key=>$v){
            if($v['frozen']==1){
                $xlsData[$key]['frozen']='冻结';
            }else{
                $xlsData[$key]['frozen']='未冻结';
            }
        }
        $filename = "用户信息";
        $headArr = array("ID","昵称","会员手机号", "冻结状态");
        $this->getExcel($filename, $headArr, $xlsData);
    }


    /**
     * 导入列表
     */
    public function implode_index()
    {
        $group_name = I('request.group_name');
        session_start();
        $phonea = $_SESSION['phoness'];
        if ($phonea !=0){
            $aa = implode(',',$phonea);
            $this->assign('phone',$aa);
            $_SESSION['phoness'] = 0;
        }else{
            $this->assign('phone', 0);
        }
        if ($group_name) {
            $where['name'] = array('like', "%$group_name%");
        }
        $count = M('group')->where($where)->count();
        $Page = new \Think\Page($count, 12);
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $Page->parameter['name'] = $group_name;
        $show = $Page->show();
        $result = M('group')->where($where)->select();
        foreach ($result as $key => $v) {
            if ($v['time']) {
                $result[$key]['formart_time'] = date('Y-m-d', $v['time']);
            } else {
                $result[$key]['formart_time'] = "";
            }
        }
        $this->assign('result', $result);
        $this->assign("page", $show);
        $this->assign('group_name', $group_name);
        $this->display();
    }

    /**
     * 下载导入文件模板
     */
    public function uploadFlie(){
        header("Content-type:text/html;charset=utf-8");
        //用以解决中文不能显示出来的问题
        $file_name='活动用户群批量导入模板.xlsx';
        $file_sub_path=$_SERVER['DOCUMENT_ROOT']."/Public/File/";
        $file_path=$file_sub_path.$file_name;
        //首先要判断给定的文件存在与否
        if(!file_exists($file_path)){
            $this->error("没有该文件");die;
        }
        $fp=fopen($file_path,"r");
        $file_size=filesize($file_path);
        //下载文件需要用到的头
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:".$file_size);
        Header("Content-Disposition: attachment; filename=".$file_name);
        $buffer=1024;
        $file_count=0;
        //向浏览器返回数据
        while(!feof($fp) && $file_count<$file_size){
            $file_con=fread($fp,$buffer);
            $file_count+=$buffer;
            echo $file_con;
        }
        fclose($fp);
    }

    /**
     * 导入内容
     */
    public function implodEdit()
    {
        $id = I('request.id');
        $name = M('group')->where("id='$id'")->getField('name');
        $this->assign('id', $id);
        $this->assign('name', $name);
        $this->display();
    }

    /**
     * 导入
     */
    public function implode()
    {
        session_start();
        $_SESSION['phoness'] = 0;
        $data = $this->import($_FILES['excel']);
        $group['group_id'] = I('request.id');
        $group['time'] = time();
        if (!empty($data)) {
            foreach ($data as $key => $v) {
                if(!preg_match("/^1[345678]\d{9}$/", $v['mobile'])){
                    $line = $key+2;
                    $this->error("文件第".$line."行出现格式不正确电话号码，请修改后重新上传");die;
                }
                $mobile =$v['mobile'];
                $group['mobile'] = $mobile;
//                $user_result= M('user')->where("mobile='$mobile'")->find();
                $where['mobile'] = $mobile;
                $where['group_id'] = $group['group_id'];
                $user_result= M('group_user')->where($where)->find();
                if(empty($user_result)){
                    $user_data['mobile']=$mobile;
                    $user_result['addtime']= $group['time'];
                    M('group_user')->add($group);
                    $phone[] = $mobile;
                }
            }
            $gwhere['id'] = $group['group_id'];
            $map['time'] = time();
            $result = M('group')->where($gwhere)->save($map);
            session_start();
            $_SESSION['phoness'] = $phone;
            $this->success("导入成功", 'Admin/User/implode_index');
        } else {
            $this->error("导入失败");
        }
    }

    /**
     * 添加群组
     */
    public function addGroup()
    {
        $data['time'] = time();
        $data['name'] = I('request.name');
        $id = I('request.id');
        if ($id) {
            $result = M('group')->where("id='$id'")->save($data);
            if ($result === false) {
                $this->error('修改失败');
            } else {
                $this->success("修改成功", 'Admin/User/implode_index');
            }
        } else {
            $result = M('group')->add($data);
            if ($result) {
                $this->success("添加成功", 'Admin/User/implode_index');
            } else {
                $this->error('添加失败');
            }
        }


    }

    /**
     * 删除群组
     */
    public function delGroup()
    {
        $id = I('request.id');
        $result = M('group')->where("id='$id'")->delete();
        if ($result) {
            $this->success("删除成功", 'Admin/User/implode_index');
        } else {
            $this->success("删除失败");
        }
    }

    /**
     * 关于我们
     */
    public function aboutUs()
    {
        $result = M('about')->order('id desc')->find();
        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 关于我们修改(增加)页面
     */
    public function editIndex()
    {
        $id = I('request.id');
        $result = M('about')->where("id='$id'")->find();
        $content = htmlspecialchars_decode($result['content']);
        if ($content){
            $result['content'] = $content;
        }
        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 修改(新增)关于我们
     */
    public function addAbout()
    {
        $content= $_POST['content'];
        if ($content){
            $data['content'] = $content;
        }
        $data['time'] = time();
        $id = I('request.id');
        if ($id) {
            $result = M('about')->where("id='$id'")->save($data);
            if ($result === false) {
                $this->error("操作失败");
            } else {
                $this->success('操作成功', 'Admin/User/aboutUs');
            }
        } else {
            $result = M('about')->add($data);
            if ($result) {
                $this->success('操作成功', 'Admin/User/aboutUs');
            } else {
                $this->error("操作失败");
            }
        }

    }

    /**
     * 删除关于我们
     */
    public function delAbout()
    {
        $id = I('request.id');
        $result = M('about')->where("id='$id'")->delete();
        if ($result) {
            $this->success("操作成功", 'Admin/User/aboutUs');
        } else {
            $this->error("操作失败");
        }

    }

}